home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9363 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  119 lines

  1. Path: news.db.erau.edu!kochank
  2. From: kochank@news.db.erau.edu (Konrad Kochan)
  3. Newsgroups: comp.lang.c
  4. Subject: PLEASE Help! :(
  5. Date: 9 Mar 1996 20:44:50 GMT
  6. Organization: Embry-Riddle Aeronautical University
  7. Message-ID: <4hsqk2$4d7@deadbird.db.erau.edu>
  8. NNTP-Posting-Host: 155.31.1.1
  9. NNTP-Posting-User: kochank
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Howdy.. I have a program to write, but I'm clueless... Now I could
  13. give you a 100 excuses and blame the instructor (and I should cause
  14. all of it would be true.. :)
  15.  
  16. Anyways, I'm clueless.. can someone, a good samaritan (or someone just
  17. bored..) help me out!
  18.  
  19. Here is the program statement..
  20.  
  21. Write a C program to sort, using any sorting method, a text file
  22. named RECORDS.DAT, of unknown length, of records organized as follows:
  23.  
  24. Field 1.  Contains an integer.
  25. Field 2.  Contains a string of exactly 20 characters.
  26. Field 3.  Contains a float followed by a newline character.
  27.  
  28. Example of file organization:
  29.  
  30. 234 Numa Chaikin        33000.0
  31.  75 advertisements      55.55
  32. 901 Shepard             15255.5
  33.   3 Brook               120050.0
  34. 888 non-descriptive     0.0
  35.  
  36. Sorting has to be done lexicographically, that is, by the second field
  37. using alphabetical order.  This means that:
  38.  
  39. 1. Upper-case and lower-case letters should be treated identically.
  40.    For example, the right order for a part of the list above is:
  41.    888 non-descriptive 0.0
  42.    234 Numa Chaikin 33000.0
  43.  
  44. 2. The only punctuation sign used in strings is a dash '-', which
  45.    should be ignored in sorting (but not removed from words).
  46. And here is what I got so far.. which DOESN'T work (right or otherwise, I
  47. can't even get the program to scan the right fields from the file.. :(
  48.  
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52.  
  53. /* Global Variable Definition */
  54.  
  55. int IntArr[512][5];
  56. char WordsArr1[512][10];
  57. char WordsArr2[512][10];
  58. float FloatArr[512][5];
  59. char inbuff[80];
  60. int index=0;
  61.  
  62. /* Function declaration */
  63.  
  64. void Welcome(void);
  65. void Goodbye(void);
  66.  
  67. main()
  68. {
  69. /* Next function opens a file, if the file is missing, it prints
  70.  an error message */
  71.  
  72. int i;
  73.  FILE * fin, * fout;
  74.     if (!(fin = fopen("RECORDS.DAT", "r")))
  75.      {
  76.     printf("Something is wrong: Probably file missing.\n");
  77.     exit(1);   /* Program exits here if file is missing. */
  78.             }
  79.     printf("\nInput File = RECORDS.DAT\n");
  80. /* This function prints a Welcome message */
  81.  
  82. /* Welcome(); */
  83.  
  84. /* This function scans in the array */
  85.  
  86.     while (fgets(inbuff,80,fin) !=NULL)
  87. {
  88. sscanf(inbuff,"%d %s %s %4.2f", &IntArr[index],&WordsArr1[index],&WordsArr2[index],&FloatArr[index]);
  89. index++;
  90. }
  91.  
  92. /* Opens the output file */
  93.  
  94. if (!(fout = fopen("test", "w")))
  95.      {
  96.     printf("Something is wrong: File might be write protected.\n");
  97.     exit(1);   /* Program exits here if file can't be opened. */
  98.             }
  99.  
  100.     for (i=0; i<index; i++)
  101.     fprintf(fout, "%d %s %s %4.2f\n",  IntArr[i], WordsArr1[i],WordsArr2[i], 
  102.      FloatArr[i]);
  103.         
  104. /* Prints the sorted array to the screen */
  105.     
  106.     for (i=0; i<index; i++)
  107.     printf("Original array: %d %s %s %4.2f \n", IntArr[i],WordsArr1[i],
  108. WordsArr2[i],FloatArr[i]);
  109. fclose(fin);
  110. fclose(fout);
  111. }
  112. If you can either give me hints, rewrite the above, or write a totaly
  113. new program I would appreciate it.. I know its a lot to ask for (to do some-
  114. one elses homework pretty much..) but I'm really clueless..
  115.  
  116. Thank you in advance!
  117.  
  118.  
  119.